const { HttpException } = require('../utils/http-exception');

const catchError = async (ctx, next) => {
    try {
        await next();
    }catch(error) {
        if(error instanceof HttpException){
            // console.log('111')
            ctx.body = {
                msg: error.msg,
                errorCode: error.errorCode,
                request: `${ctx.method}  ${ctx.path}`
            }
            // ctx.status = error.code;
        }else {
            // if(global.config.environment === 'dev') throw error
            ctx.body = {
                msg: "未知异常",
                error_code: 9999,
                request: `${ctx.method}  ${ctx.path}`
            }
            ctx.status = 500
            throw error
            // ctx.body = {
            //     msg: "未知异常",
            //     error_code: 9999,
            //     request: `${ctx.method}  ${ctx.path}`
            // }
            // ctx.status = 500
        }
    }
}

// 面向切片思维

module.exports = catchError;